// pages/pay/myVouchers/myVouchers1.js
const app = getApp()
const WXAPI = require("../../../api/api")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    merchantId: {
      type: String,
      value: '',
    },
    amount:{
      type: Number,
      value: '',
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    path: 'type',//type:显示我的券包   vouchers：显示可使用优惠券   dyabulk和wxabulk：显示可核销团购订单   code：显示核销码
    imgHttp: app.globalData.imgHttp, //图片前缀
    vouData: {},
    vouchersList: [],//优惠券列表
    grouponList: [],//团购列表
    codeData: {},
  },
  attached(){
    // this.setData({
    //   merchantId: '1428651644106575874'
    // })
    this.getPackage()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //关闭我的券包弹窗
    handleChose(){
      this.triggerEvent("myVouchersChose")
    },
    //关闭优惠券弹窗
    choseVouchers(){
      this.setData({
        path: 'type'
      })
    },
    //打开优惠券, 团购
    handleSecondary(e){
      const path = e.currentTarget.dataset.path
      if(path == 'wxabulk'){
        this.getWxgroupon()
      }else if(path == 'dyabulk'){
        this.getDygroupon()
      }else if(path == 'vouchers'){
        this.getVouchers()
      }else if(path == 'code'){
        
      }
      this.setData({
        path: path
      })
    },
    getPackage(){
      var data = {
        merchantId: this.data.merchantId
      }
      WXAPI.assetCount(data, (res) => {
        this.setData({
          vouData: res.result,
        })
      })
    },
    //获取可用优惠券
    getVouchers(){
      var data = {
        pageNum: 1,
        pageSize: 100,
        merchantId: this.data.merchantId
      }
      WXAPI.availableVouList(data, (res) => {
        this.setData({
          vouchersList: res.result.records,
        })
      })
    },
    //获取卡包团购
    getWxgroupon(){
      var data = {
        pageNum: 1,
        pageSize: 100,
        merchantId: this.data.merchantId
      }
      WXAPI.availableWxgroupon(data, (res) => {
        this.setData({
          grouponList: res.result.records,
        })
      })
    },
    //获取抖音团购
    getDygroupon(){
      var data = {
        pageNum: 1,
        pageSize: 100,
        merchantId: this.data.merchantId
      }
      WXAPI.availableDygroupon(data, (res) => {
        this.setData({
          grouponList: res.result.records,
        })
      })
    },
    //团购券立即使用
    handleUse(e){
      const path = e.currentTarget.dataset.path
      const id = e.currentTarget.dataset.id
      this.setData({
        path: path
      })
      this.getCode(id)
    },
    //查看团购核销二维码
    getCode(id){
      var url = `/app/v1/merchant/groupon/detail/${id}`
      WXAPI.abulkDetailCode(url,'',(res) => {
        this.setData({
          codeData: res.result,
        })
      })
    },
    //复制
    copy(){ //复制
      wx.setClipboardData({
        data: this.data.codeData.code,
        success (res) {
          wx.showToast({
            title: '复制成功',
          })
        }
      })    
    },

    //使用优惠券
    vouchersUse(e){
      const item = e.currentTarget.dataset.item
      if(this.data.amount < item.preferentialCondition){
        var title = `很抱歉该优惠券[满${item.preferentialCondition}]可用,请先修改支付金额`
        // if(item.couponType == 1){
        //   var title = `很抱歉【满${item.preferentialCondition}减${item.preferentialDiscount}】优惠券无法在该店铺使用`
        // }else if(item.couponType == 2){
        //   var title = `很抱歉【满${item.preferentialCondition}打${item.preferentialDiscount}折】优惠券无法在该店铺使用`
        // }
        
        wx.showToast({
          title: title,
          icon: 'none'
        })
      }else{
        this.handleChose()
        this.triggerEvent('vouchersUse',item)
      }
      
    },
  }
})
